package org.dtrd.modules.service.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.service.entity.bean.Executor;
import org.dtrd.modules.service.entity.bean.ServiceDetail;
import org.dtrd.modules.service.entity.bean.ServiceListInfo;
import org.dtrd.modules.service.entity.po.DtrdEntRdServicePackage;
import org.dtrd.modules.service.service.IDtrdEntRdServicePackageService;
import org.dtrd.modules.service.service.IDtrdEntRdServiceService;
import org.dtrd.modules.service.service.IDtrdRlPatientServiceDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;

/**
 * <p>
 * 服务套餐信息 前端控制器
 * </p>
 *
 * @author QR
 * @since 2021-11-19 18:52:54
 */
@Api(tags = "服务套餐")
@RestController
@RequestMapping("/107/03/")
public class ServiceController extends BaseController {

    @Autowired
    private IDtrdEntRdServiceService serviceService;
    @Autowired
    private IDtrdEntRdServicePackageService packageService;
    @Autowired
    private IDtrdRlPatientServiceDetailService patientServiceDetailRlService;
    @Autowired
    private BaseCommonService baseCommonService;


    @ApiOperation("添加或修改服务套餐")
    @PostMapping("1070301")
    @AutoLog(value="添加或修改服务套餐", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateService(@Validated @RequestBody ServiceDetail serviceDetail) {
        String serviceName = serviceDetail.getServiceName();
        Integer serviceId = serviceDetail.getDataId();
        if (serviceService.hasSameServiceName(serviceId, serviceName, serviceDetail.getOrgCode())) {
            return Result.error("不能用相同名字的服务套餐");
        }

        // if (serviceId != null && patientServiceDetailRlService.hasServiceRelateToPatient(serviceId)) {
        //     return Result.error("有正在进行中的服务, 不可编辑");
        // }

        boolean result = serviceService.saveOrUpdateServiceInfo(serviceDetail);
        String logContent = String.format("添加或修改服务套餐, 编辑内容: %s", serviceDetail);
        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }


    @ApiOperation("获取服务套餐列表")
    @GetMapping("1070302")
    @AutoLog(value="获取服务套餐列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getServiceList(@PathParam("packageId") Integer packageId,
                                    @PathParam("orgCode") String orgCode) {
        List<ServiceListInfo> serviceListInfoList = serviceService.getServiceList(packageId, orgCode);
        JSONObject result = new JSONObject();
        result.put("serviceList", serviceListInfoList);
        if (packageId != null && packageId != 0) {
            DtrdEntRdServicePackage servicePackage = packageService.getById(packageId);
            result.put("packageType", servicePackage.getPackageType());
            result.put("packagePeriod", Optional.ofNullable(servicePackage.getPackageServicePeriod()).orElse(0));
        }
        String logContent = String.format("获取服务套餐列表, 获取服务包id: %d", packageId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
        return Result.OK(result);
    }


    @ApiOperation("查看服务套餐详情")
    @GetMapping("1070303")
    @AutoLog(value="查看服务套餐详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getServiceInfo(@RequestParam Integer serviceId) {
        ServiceDetail serviceDetail = serviceService.getServiceInfo(serviceId);
        String logContent = String.format("查看服务套餐详情, 获取服务详情id: %d", serviceId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
        return Result.OK(serviceDetail);
    }


    @ApiOperation("删除服务套餐")
    @DeleteMapping("/1070305")
    @AutoLog(value="删除服务套餐", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeService(@RequestParam Integer serviceId) {
        // if (patientServiceDetailRlService.hasServiceRelateToPatient(serviceId)) {
        //     return Result.error("有正在进行中的服务, 无法删除");
        // }
        boolean result = serviceService.removeService(serviceId);
        String logContent = String.format("删除服务套餐, 删除服务详情id: %d", serviceId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("获取执行人列表")
    @GetMapping("/1070319")
    public Result<?> getExecutorList(){
        Constant.Executor[] values = Constant.Executor.values();
        ArrayList<Executor> executors = new ArrayList<>(values.length);
        for (Constant.Executor executor : values) {
            String label = executor.getName();
            int value = executor.getType();
            if (value != 0) {
                executors.add(new Executor(label, value));
            }
        }
        return Result.OK(executors);
    }
}
